package com.xrz.utils;


import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.UUID;


public class FileUpload {
    public static String saveFile(MultipartFile file) throws IOException {
        //获得SpringBoot当前项目的路径：System.getProperty("user.dir")
//        String path = System.getProperty("user.dir") + "/upload/"; //Windows
        String path = "/home/xrz/qaq/upload/"; //linux
        File realPath = new File(path);
        if (!realPath.exists()) {
            realPath.mkdir();
        }
        //上传文件地址
        //解决文件名字问题：我们使用uuid;
        String filename =  UUID.randomUUID().toString().replaceAll("-", "");
        String originalFilename = file.getOriginalFilename();//文件原名
        assert originalFilename != null;
        int i = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(i + 1);
        String outFilename = filename + "." + suffix;//保存的文件名
        file.transferTo(new File(realPath +"/"+ outFilename));
        String f="/home/xrz/qaq/upload/"+ outFilename;
        System.out.println(realPath);
        return f;
    }
}

